/**
 * @author Bernhard Denner
 * @author Maximilian Csuk
 * @author Konrad Paumann
 */
public class CTree<Node, Edge> extends Tree<Node, Edge> {

	public CTree(Node nodeDesc) {
		super(nodeDesc);
	}
	
	/**
	 * tries to find a node, which's title is equal to the given title (n)
	 * and if found returns the node, 
	 * otherwise it returns null
	 */
	private Tree<Node, Edge> find(Tree<Node, Edge> root, Node n) {
		TreeNodeIterator<Node, Edge> nit = root.getNodeIterator();
		
		if (root.getNodeTitle().equals(n))
			return root;
		else {
			while(nit.hasNext()) {
				Tree<Node, Edge> node = find(nit.next(), n);
				
				if (node != null)
					return node;
			}
		}
		return null;
	}
	/**
	 * Checks if two nodes a and b are connected, 
	 * node a and b are connected if b is a node of the subtree of a
	 * Returns true if yes, false otherwise
	 */
	public boolean connected(Node a, Node b) {
		Tree<Node, Edge> na = find(this, a);
		if (na == null) 
			return false;
		Tree<Node, Edge> nb = find(na, b);
		if (nb == null) 
			return false;
		return true; 
	}

}
